/*
 * @Descripttion: |
 * @version: 1.0
 * @Author: 王娆
 * @Date: 2020-08-23 17:35:04
 * @LastEditors: 王娆
 * @LastEditTime: 2020-08-27 19:58:01
 */
/*
 * @Descripttion: |
 * @version: 1.0
 * @Author: 王娆
 * @Date: 2020-08-23 01:35:55
 * @LastEditors: 王娆
 * @LastEditTime: 2020-08-27 19:41:52
 */
/* eslint-disable */
import axios from 'axios'
import cookie from 'js-cookie'
import router from '../router'
import ErrorCode from '../config/error'
import {Message} from 'element-ui'
const writeList = ['/login']

axios.defaults.baseURL = 'http://localhost:8080/api'

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  const token = cookie.get('token')
  if (!writeList.includes(config.url)) {
    if (!token) {
      router.replace('/login')
    }
    config.headers.authorization = token
  }
  // 在发送请求之前做些什么
  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error)
})

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response
}, function (error) {
  // 对响应错误做点什么
  Message.error(ErrorCode[error.response.status])
})

export default axios
